package com.tanhua.manage.controller;

import com.tanhua.manage.pojo.AdminUser;
import com.tanhua.manage.pojo.Role;
import com.tanhua.manage.service.AdminUserService;
import com.tanhua.manage.util.NoAuthorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Au
 * @DATE 2020/12/14 0:23
 **/
@RestController
@RequestMapping("/admin/user")
public class AdminUserController {
    @Autowired
    private AdminUserService adminUserService;

    @GetMapping
    @NoAuthorization
    public ResponseEntity<List<AdminUser>> queryAdminUser() {

        List<AdminUser> adminUser = adminUserService.queryAdminUser();

        if (adminUser != null) {
            return ResponseEntity.ok(adminUser);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("/query")
    @NoAuthorization
    public ResponseEntity<List<AdminUser>> queryRoleByRoleName(@RequestParam(value = "adminUserName", required = false) String adminUserName) {

        List<AdminUser> adminUser = adminUserService.queryAdminUserAdminUserName(adminUserName);

        if (adminUser != null) {
            return ResponseEntity.ok(adminUser);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @DeleteMapping("/delete/{id}")
    @NoAuthorization
    public ResponseEntity<Void> deleteAdminUserById(@PathVariable("id") Long id) {
        boolean flag = adminUserService.deleteAdminUserById(id);
        if (flag) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PostMapping("/save")
    @NoAuthorization
    public ResponseEntity<Void> saveAdminUser(@RequestBody AdminUser adminUser) {
        boolean flag = adminUserService.saveAdminUser(adminUser);
        if (flag) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PutMapping("/update")
    @NoAuthorization
    public ResponseEntity<Void> updateAdminUser(@RequestParam(value = "adminUserId", required = false) Long adminUserId,
                                                @RequestParam(value = "phone", required = false) String phone,
                                                @RequestParam(value = "roleId", required = false) Long roleId) {
        boolean flag = adminUserService.updateAdminUser(adminUserId, phone,roleId);
        if (flag) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PutMapping("/reset")
    @NoAuthorization
    public ResponseEntity<Void> resetPassWord(@RequestParam(value = "adminUserId", required = false) Long adminUserId,
                                                @RequestParam(value = "passWord", required = false) String passWord) {
        boolean flag = adminUserService.resetPassWord(adminUserId, passWord);
        if (flag) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
